﻿@{
    ViewBag.Title = "查询分析器";
    Layout = "~/Views/Shared/_Main.cshtml";
}
<style>
    #Layer1
    {
        display: none;
        position: absolute;
        width: 240px;
        border: 1px solid #cccccc;
        background-color: #ffffff;
        line-height: 18px;
        height: 200px;
        padding: 3px;
        overflow: scroll;
    }
    #divSoutceTableData
    {
        width: 100%;
        height: 400px;
        overflow: scroll;
    }
    .pagTable td
    {
        padding-left: 3px;
    }
    .lover
    {
        background-color: #efefef;
    }
</style>
<script type="text/javascript">
    document.onclick = function () {
        $("#Layer1").hide();
    };
    $(function () {
        $.post('/SysMenu/DBList', function (result) {
            for (var i = 0; i < result.length; i++) {
                $("#sel_DBName").append("<option value='" + result[i].DBName + "'>" + result[i].DisplayName + "</option>");
            }
        });
    });

    function selectSourceList() {
        showModalMany('SelectSourceList', '/SystemConfiguration/SelectSourceList?selectCount=1&dbName=' + encodeURI($("#sel_DBName").val()), '810', '460',
                function (v) {
                    if (typeof (v) == "undefined") return;
                    if (v.length > 0) {
                        $('.txt_SourceSysName').val(v[0].SourceSysName);
                        $('#sel_SourceSysGuid').val(v[0].GUID);
                    }
                });
    }
    function selectTargetList() {
        showModalMany('SelectTargetList', '/EtlTarget/SelectTargetList?selectCount=1&dbName=' + encodeURI($("#sel_DBName").val()), '810', '460',
                function (v) {
                    if (typeof (v) == "undefined") return;
                    if (v.length > 0) {
                        $('.txt_TargetSysName').val(v[0].TargetSysName);
                        $('#sel_TargetSysGuid').val(v[0].GUID);
                    }
                });
    }

    var postPara;
    function GetSql() {
        if ($("#sel_SourceSysGuid").val() == "" && $("#sel_TargetSysGuid").val() == "") {
            $("#Layer1").hide();
            var isEnter;
            var sql = $("#txt_sql").val().toLowerCase()
            sql = sql.replace("\r\n", " ");
            var lastf = sql.lastIndexOf(" ");
            if (sql.indexOf(" ") > 0 && sql.length - lastf >= 1) {
                var pushSQL = sql.substr(lastf + 1);
                if (pushSQL.indexOf(" ") == -1) {
                    var sqlArr = pushSQL.split(".");
                    var s;
                    var t;
                    var c;
                    if (sqlArr.length > 0)
                        postPara = s = sqlArr[0];
                    if (sqlArr.length > 1)
                        postPara = t = sqlArr[1];
                    if (sqlArr.length > 2)
                        postPara = c = sqlArr[2];
                    $.post("/PEPara/GetObjects", { s: s, t: t, c: c, l: sqlArr.length, DBName: $("#sel_DBName").val() }, function (res) {
                        $("#Layer1").html(res);
                        showdiv();
                    });
                }
            } 
        }
    }

    function initSourceTableData(sortField, sortBy, query) {
        $("#loading").show();
        $.post("/PEPara/RunSQL", { RunSQL: $("#txt_sql").val(), sourceID: $("#sel_SourceSysGuid").val(), targetID: $("#sel_TargetSysGuid").val(), DBName: $("#sel_DBName").val(), order: sortField, sort: sortBy }, function (result) {
            $("#divSoutceTableData").html(result);
            $("#loading").hide();
        });
    }
    function showdiv() {
        var rng = document.selection.createRange();
        $("#Layer1").css("left", rng.offsetLeft);
        $("#Layer1").css("top", rng.offsetTop + 20);
        $("#Layer1").show();
    }
    function lover(c) {
        $("." + c).addClass("lover");
    }
    function lout(c) {
        $("." + c).removeClass("lover");
    }

    function btnSearch_Clear() {
        $(".txt_SourceSysName").val("");
        $("#sel_SourceSysGuid").val("");
        $(".txt_TargetSysName").val("");
        $("#sel_TargetSysGuid").val("");
    }
    function lclick(c) {
        var txtSql = $("#txt_sql").val();
        txtSql = txtSql.substr(0, txtSql.lastIndexOf(postPara));
        $("#txt_sql").val(txtSql + $("." + c).html());
        $("#txt_sql").focus();
        $("#Layer1").hide();
    }
</script>
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/search.png" />
            </td>
            <td width="300">
                查询分析器
            </td>
            <td align="right">
                <input type="button" class="button" value="执行" onclick="initSourceTableData()" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td>
                选择实例：
                <select id="sel_DBName">
                </select>
                源系统： <span style="width: 150px" class="combo datebox">
                    <input disabled="disabled" style="width: 125px" class="combo-text validatebox-text txt_SourceSysName"
                        type="text" />
                    <span><span class="combo-arrow" style="background: url(/images/view.png) no-repeat center center"
                        onclick="selectSourceList()"></span></span>
                    <input id="sel_SourceSysGuid" class="combo-value" type="hidden" /></span> 对接系统：
                <span style="width: 150px" class="combo datebox">
                    <input disabled="disabled" style="width: 125px" class="combo-text validatebox-text txt_TargetSysName"
                        type="text" />
                    <span><span class="combo-arrow" style="background: url(/images/view.png) no-repeat center center"
                        onclick="selectTargetList()"></span></span>
                    <input id="sel_TargetSysGuid" class="combo-value" type="hidden" /></span>
                <input type="button" class="button" value=" 清空条件 " onclick="btnSearch_Clear()" />
                <br />
                <textarea id="txt_sql" onkeyup="GetSql();" rows="5" style="width: 1030px; margin-top: 5px;"></textarea>
                <div id="divSoutceTableData" style="width: 1036px;">
                </div>
            </td>
        </tr>
    </table>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
    <div id="Layer1">
    </div>
</div>
